package com.hjh.seata.user.controller;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("account/reduce")
    @GlobalTransactional(rollbackFor = Exception.class)
    public String reduceAccount() {
         jdbcTemplate.update("INSERT INTO `seata_user`.`t_user` (`account`) VALUES ( "+ RandomUtils.nextDouble()+");");
        log.info("=================================>>>user");

        restTemplate.getForEntity("http://localhost:8081/good-service/good/amount/reduce", String.class);

        int k = RandomUtils.nextInt()%2;
        log.info("==============kkkkk: " + k);
       int k1 =  10/k;

        return "success";
    }
}